﻿using Inovout.Memberships.Models;
using Inovout.Repositories;
using Inovout.Security;
using NHibernate.Criterion;
using NHibernate.Linq;
using System.Linq;

namespace Inovout.Memberships.Repositories
{
    public class MembershipUserRepository : DataObjectRepository<MembershipUser>
    {
        public MembershipUser FindByClaimedIdentifier(ClaimedIdentifier claimedIdentifier)
        {
            return base.Find(Restrictions.Eq("IdentifierCode", claimedIdentifier.Code).IgnoreCase());
        }

        internal IMembershipUser FindByByUserNameOrEmailForLocal(string userName,string email)
        {
            FormsUserCredential formsUserCredential = ComponentRegistry.Resolve<IRepository<FormsUserCredential>>().Find(Restrictions.Eq("UserName", userName).IgnoreCase());
            if (formsUserCredential != null)
            {
                return FindBySystemUser(formsUserCredential.SystemUser);
            }

            UserCredential userCredential = ComponentRegistry.Resolve<IRepository<UserCredential>>().Find(Restrictions.Eq("Email", email).IgnoreCase());
            if (userCredential != null)
            {
                return FindBySystemUser(userCredential.SystemUser);
            }
            return null;
        }

        internal IMembershipUser FindByUserNameOrEmail(string userName)
        {
            FormsUserCredential formsUserCredential = new FormsUserCredential();
            formsUserCredential = ComponentRegistry.Resolve<IRepository<FormsUserCredential>>().Find(Restrictions.Eq("UserName", userName).IgnoreCase());
            if (formsUserCredential != null)
            {
                return FindBySystemUser(formsUserCredential.SystemUser);
            }
         
           var userCredential = ComponentRegistry.Resolve<IRepository<UserCredential>>().Find(Restrictions.And(Restrictions.Eq("Provider", "Inovout").IgnoreCase(), Restrictions.Eq("Email", userName).IgnoreCase()));
           if (userCredential != null)
            {
                return FindBySystemUser(userCredential.SystemUser);
            }
            return null;
        }

        internal IMembershipUser FindByUsingIdentifierForeach(string identifier)
        {
            FormsUserCredential formsUserCredential = ComponentRegistry.Resolve<IRepository<FormsUserCredential>>().Find(Restrictions.Eq("UserName", identifier).IgnoreCase());
            if (formsUserCredential != null)
            {
                return FindBySystemUser(formsUserCredential.SystemUser);
            }

            OpenIdUserCredential openIdUserCredential = ComponentRegistry.Resolve<IRepository<OpenIdUserCredential>>().Find(Restrictions.Eq("Identifier", identifier).IgnoreCase());
            if (openIdUserCredential != null)
            {
                return FindBySystemUser(openIdUserCredential.SystemUser);
            }

            OAuthUserCredential oauthUserCredential = ComponentRegistry.Resolve<IRepository<OAuthUserCredential>>().Find(Restrictions.Eq("Code", identifier).IgnoreCase());
            if (oauthUserCredential != null)
            {
                return FindBySystemUser(oauthUserCredential.SystemUser);
            }

            return null;
        }



        internal IMembershipUser FindByProviderIdentifier(string provider, string identifier)
        {
            //DataObjectRepository<UserCredential> userCredentialDataObject = new DataObjectRepository<UserCredential>();
            // UserCredential userCredential = userCredentialDataObject.Query().SingleOrDefault(m => m.Provider == provider && m.Identifier == identifier);

            UserCredential userCredential = ComponentRegistry.Resolve<IRepository<UserCredential>>().Find(Restrictions.And(Restrictions.Eq("Provider", provider).IgnoreCase(), Restrictions.Eq("Identifier", identifier).IgnoreCase()));

            if (userCredential != null)
            {
                return FindBySystemUser(userCredential.SystemUser);
            }
            return null;
        }

       
        internal IMembershipUser FindBySystemUser(SystemUser systemUser)
        {

            return base.Query().SingleOrDefault(m => m.SystemUser == systemUser);
        }

        internal IMembershipUser FindBySystemUserName(string name)
        {
            return base.Query().SingleOrDefault(m => m.SystemUser.Name == name);
        }

        //internal IMembershipUser FindByEmail(string email)
        //{
        //    UserCredential userCredential = ComponentRegistry.Resolve<IRepository<UserCredential>>().Find(Restrictions.Eq("Email", email));
        //    if (userCredential != null)
        //    {
        //        return FindBySystemUser(userCredential.SystemUser);
        //    }
        //    return null;
        //}

        public IMembershipUser FindByMembership(IMembership membership)
        {
            return base.Query().SingleOrDefault(m => m.Membership == membership);
        }
    }
}
